package com.haidechizi.leetcode;

public class _45_跳跃游戏II {

    public int jump(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }

        if (nums.length == 1) {
            return 0;
        }
        int maxIndex = 0;
        int currentIndex = 0;
        int count = 0;
        while (currentIndex < maxIndex + nums[maxIndex] && currentIndex < nums.length) {
            if (maxIndex + nums[maxIndex] >= nums.length - 1) {
                break;
            }
            int max = 0;
            while (currentIndex < maxIndex + nums[maxIndex] && currentIndex < nums.length) {
                if (nums[currentIndex] + currentIndex > nums[max] + max) {
                    max = currentIndex;
                }
                currentIndex++;
            }
            if (max + nums[max] > maxIndex + nums[maxIndex]) {
                maxIndex = max;
                count++;

            }
        }
        if(maxIndex != nums.length - 1) {
            count++;

        }
        return count;

    }
}
